import { getList } from '@/api/article';
import { ApiCode } from '@/types/api/base';
import { Article } from '@/types/service/article';

export default (page = ref(1), limit = ref(10),) => {
    const state = reactive({
        isloading: false,
        list: [] as Article[],
        total: 0,
        page: unref(page),
        limit: unref(limit),
    })

    const run = async () => {
        state.isloading = true
        const res = await getList({ page: unref(state.page) })
        if (res.code === ApiCode.success) {
            state.page += 1
            state.list = [
                ...state.list,
                ...res.data.list
            ]
        }
        state.isloading = false
    }

    /**
     * 设置第几页
     * @param page 
     */
    const setPage = (page: number) => {
        state.page = page
    }

    /**
     * 设置每页数量
     * @param limit 
     */
    const setLimit = (limit: number) => {
        state.limit = limit
    }
    onMounted(() => {
        run()
    })

    return {
        run,
        setPage,
        setLimit,
        ...toRefs(state)

    }
}